package com.example.baselibrary.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.media.FaceDetector;
import android.os.Build;
import android.util.Log;

import com.example.baselibrary.Interface.FacePickInterface;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import static android.graphics.BitmapFactory.Options;
import static android.graphics.BitmapFactory.decodeByteArray;

public final class FaceCj {
    private static Options BitmapFactoryOptionsbfo;
    private static ByteArrayOutputStream out;
    private static byte[] data;
    private static FaceDetector.Face[] myFace;
    private static FaceDetector myFaceDetect;

    private static int tx = 0;
    private static int ty = 0;
    private static int bx = 0;
    private static int by = 0;
    private static int width = 0;
    private static int height = 0;
    private static float wuchax = 0;
    private static float wuchay = 0;
    private static FaceDetector.Face face;
    private static PointF myMidPoint;
    private static float myEyesDistance;
    private static List<String> facePaths;
    private static String facePath;
    private static float[] faceSize;
    private static FaceMasks fmMask;
    private static FacePickInterface faceAppear;

    public static Bitmap cutFace(Bitmap bitmap, Context context) {
        BitmapFactoryOptionsbfo = new Options();
        BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; // 构造位图生成的参数，必须为565。类名+enum
        out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        data = out.toByteArray();

//        Bitmap bitmap1 = MyBitmapUtils.compressBitmap(bitmap, true, 1024 * 50);
//        Bitmap bitmap1 = MyBitmapUtils.compressBitmap(bitmap, false, 120, 160);
//        bitmap = bitmap1;
        bitmap = decodeByteArray(data, 0, data.length, BitmapFactoryOptionsbfo);
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        myFace = new FaceDetector.Face[1]; // 分配人脸数组空间
        myFaceDetect = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 1);
        int numberOfFaceDetected = myFaceDetect.findFaces(bitmap, myFace);
        if (numberOfFaceDetected <= 0) {// FaceDetector构造实例并解析人脸
            faceAppear.noFace();
            bitmap.recycle();
            return null;
        }
        Log.d("face", numberOfFaceDetected + "");


        faceSize = new float[numberOfFaceDetected];
        for (int i = 0; i < numberOfFaceDetected; i++) {
            faceSize[i] = myFace[i].eyesDistance();

        }
        int targetIndex = findMaxIndex(faceSize);

        face = myFace[targetIndex];


        myMidPoint = new PointF();
        face.getMidPoint(myMidPoint);

        myEyesDistance = face.eyesDistance();   //得到人脸中心点和眼间距离参数，并对每个人脸进行画框

        wuchax = myEyesDistance / 2 + myEyesDistance;
        wuchay = myEyesDistance * 2 / 3 + myEyesDistance;

        if (myMidPoint.x - wuchax < 0) {//判断左边是否出界
            tx = 0;
        } else {
            tx = (int) (myMidPoint.x - wuchax);
        }
        if (myMidPoint.x + wuchax > width) {//判断右边是否出界
            bx = width;
        } else {
            bx = (int) (myMidPoint.x + wuchax);
        }
        if (myMidPoint.y - wuchay < 0) {//判断上边是否出界
            ty = 0;
        } else {
            ty = (int) (myMidPoint.y - wuchay);
        }
        if (myMidPoint.y + wuchay > height) {//判断下边是否出界
            by = height;
        } else {
            by = (int) (myMidPoint.y + wuchay);
        }

        try {
            RectF rect = new RectF(tx, ty, bx - tx, (by - ty) + 40);
            faceAppear.getFace(rect);
            System.out.println("zyf 图片大小 ：" + "  left : " + tx + "  top : " + ty + "  right : " + (bx - tx) + "  botton : " + (by - ty));
            LogUtil.v("剪切图片 有图片出去了");
            return Bitmap.createBitmap(bitmap, tx, ty, bx - tx, (by - ty) + 40);//这里可以自行调整裁剪宽高
        } catch (Exception e) {

            e.printStackTrace();
        }

//        bitmap.recycle();
        return bitmap;

    }

    public static int findMaxIndex(float[] arr) {
        float max = 0;
        int maxIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                maxIndex = i;
            }
        }
        return maxIndex;
    }

    public static int getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
            return bitmap.getByteCount();
        }
        // 在低版本中用一行的字节x高度
        return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
    }

    public static String getPrintSize(long size) {
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";
        }
    }

    // 接口回调，监听是否有人脸框信息
    public static void SetFaceRectListener(FacePickInterface facePick) {
        faceAppear = facePick;
    }
}


//public final class FaceCj {
//    private static Options BitmapFactoryOptionsbfo;
//    private static ByteArrayOutputStream out;
//    private static byte[] data;
//    private static FaceDetector.Face[] myFace;
//    private static FaceDetector myFaceDetect;
//    private static int tx = 0;
//    private static int ty = 0;
//    private static int bx = 0;
//    private static int by = 0;
//    private static int width = 0;
//    private static int height = 0;
//    private static float wuchax = 0;
//    private static float wuchay = 0;
//    private static FaceDetector.Face face;
//    private static PointF myMidPoint;
//    private static float myEyesDistance;
//    private static List<String> facePaths;
//    private static String facePath;
//    public static Bitmap cutFace(Bitmap bitmap, Context context) {
//        facePaths = null;
//        BitmapFactoryOptionsbfo = new Options();
//        BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; // 构造位图生成的参数，必须为565。类名+enum
//        out = new ByteArrayOutputStream();
//        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
//        data = out.toByteArray();
//        bitmap = decodeByteArray(data, 0, data.length,
//                BitmapFactoryOptionsbfo);
//        try {
//            out.flush();
//            out.close();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        width = bitmap.getWidth();
//        height = bitmap.getHeight();
//        myFace = new FaceDetector.Face[5]; // 分配人脸数组空间
//        myFaceDetect = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5);
//        int numberOfFaceDetected = myFaceDetect.findFaces(bitmap, myFace);
//        if (numberOfFaceDetected <= 0) {// FaceDetector构造实例并解析人脸
//            bitmap.recycle();
//            return null;
//        }
//        facePaths = new ArrayList<String>();
//        for (int i = 0; i < numberOfFaceDetected; i++) {
//            face = myFace[i];
//            myMidPoint = new PointF();
//            face.getMidPoint(myMidPoint);
//            myEyesDistance = face.eyesDistance();   //得到人脸中心点和眼间距离参数，并对每个人脸进行画框
//            wuchax = myEyesDistance / 2 + myEyesDistance;
//            wuchay = myEyesDistance * 2 / 3 + myEyesDistance;
//
//            if (myMidPoint.x - wuchax < 0) {//判断左边是否出界
//                tx = 0;
//            } else {
//                tx = (int) (myMidPoint.x - wuchax);
//            }
//            if (myMidPoint.x + wuchax > width) {//判断右边是否出界
//                bx = width;
//            } else {
//                bx = (int) (myMidPoint.x + wuchax);
//            }
//            if (myMidPoint.y - wuchay < 0) {//判断上边是否出界
//                ty = 0;
//            } else {
//                ty = (int) (myMidPoint.y - wuchay);
//            }
//            if (myMidPoint.y + wuchay > height) {//判断下边是否出界
//                by = height;
//            } else {
//                by = (int) (myMidPoint.y + wuchay);
//            }
//
//            try {
//                return Bitmap.createBitmap(bitmap, tx, ty, bx - tx, by - ty+5);//这里可以自行调整裁剪宽高
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        bitmap.recycle();
//        return bitmap;
//    }
//
//}
